﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ByHand.Services.Interfaces;
using ByHand.Model;
using ByHand.Services.Code;

namespace BackEnd.Controllers
{
    public class OrderController : Controller
    {
        //
        // GET: /Order/
        private readonly IOrderService _OrderService;
        private readonly IProductService _ProductService;
        private readonly ICityService _CityService;
        private readonly IDistrictService _DistrictService;
        private readonly IOrderDetailService _OrderDetailService;
        private readonly ILogService _LogService;

        public OrderController(IOrderService OrderService, IProductService ProductService, ICityService CityService, IDistrictService DistrictService, IOrderDetailService OrderDetailService, ILogService LogService)
        {
            _OrderService = OrderService;
            _ProductService = ProductService;
            _CityService = CityService;
            _DistrictService = DistrictService;
            _OrderDetailService = OrderDetailService;
            _LogService = LogService;
        }
        public ActionResult Index(string page,string filter, string status)
        {
            if (string.IsNullOrWhiteSpace(page))
            {
                page = "1";
            }
            if (string.IsNullOrWhiteSpace(status))
            {
                status = "-1";
            }
            ViewData["tableindex"] = new { page = page, pagesize = Convert.ToInt32(Resources.Resource.ItemPerPage), filter = filter, status = status };
            return View();
        }
        public ActionResult TableOrder(int page, int pagesize, string filter, int status)
        {
            int total = 0;
            pagesize = Convert.ToInt32(Resources.Resource.ItemPerPage);
            if (page == 0)
            {
                page = 1;
            }
            var model = new List<Order>();

            if (!string.IsNullOrWhiteSpace(filter))
            {
                if (status != -1)
                {
                    model = _OrderService.GetByStatusFilter(page, pagesize, out total, filter,status);
                    ViewData["filter"] = filter;
                }
                else {
                    model = _OrderService.GetByPageFilter(page, pagesize, out total, filter);
                    ViewData["filter"] = filter;
                }
            }
            else
            {
                if (status != -1)
                {
                    model = _OrderService.GetByStatus(page, pagesize, out total, status);
                }
                else {
                    model = _OrderService.GetByPage(page, pagesize, out total);
                }
            }
            if ((total % pagesize) > 0)
            {
                ViewData["total"] = (total / pagesize) + 1;
            }
            else
            {
                ViewData["total"] = total / pagesize;
            }

            ViewData["curpage"] = page;
            ViewData["nextpage"] = page + 1;
            ViewData["prepage"] = page - 1;
            ViewData["status"] = status;
            foreach (var item in model)
            {
                item.Product = _ProductService.GetById(item.ProductID);
                item.ToCity = _CityService.GetById(item.ToCityID);
                item.ToDistrict = _DistrictService.GetById(item.ToDistrictID);
                item.OrderDetail = _OrderDetailService.GetByOrder(item.ID);
            }
            return PartialView(model);
        }
        public ActionResult Detail(int id) {
            var model = _OrderService.GetById(id);
            model.Product = _ProductService.GetById(model.ProductID);
            model.OrderDetail = _OrderDetailService.GetByOrder(model.ID);
            return PartialView(model);
        }
        public ActionResult Update(int status,string toName,string toPhone,string toEmail,string toAddress,int oID) {
            int actionDetail = -1;
            var model = _OrderService.GetById(oID);
            if (model.Status != status)
            {
                actionDetail = (int)ELogActionDetail.Update_Status;
            }
            else {
                if (model.ToEmail != toEmail || model.ToPhone != toPhone || model.ToAddress != toAddress) {
                    actionDetail = (int)ELogActionDetail.Update_Information;
                }
            }
            model.Status = status;
            model.ToName = toName;
            model.ToPhone = toPhone;
            model.ToEmail = toEmail;
            model.ToAddress = toAddress;
            model.UserModified = "admin";
            model.ModifiedDate = DateTime.Now;
            _OrderService.Update(model);
            _LogService.Add(new Log() { 
                OrderID = model.ID,
                DateTime = DateTime.Now,
                ActionType = (int)ELogActionType.Admin_Update,
                ActionDetail = actionDetail,
                ProductID = model.ProductID,
                UserName = "Admin"
            });
            return Json(true);
        }
    }
}
